package com.example.memo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.memo.db.UserHelper;

public class Register extends AppCompatActivity implements View.OnClickListener {
    private TextView back;
    private EditText et_account;
    private EditText et_password;
    private Button btn_register;
    private UserHelper userHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initial();
    }

    private void initial() {
        et_account = findViewById(R.id.account);
        et_password = findViewById(R.id.password);
        btn_register = findViewById(R.id.register);
        back = findViewById(R.id.back);
        userHelper = new UserHelper(this);

        back.setOnClickListener(this);
        btn_register.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if(view == back){
            finish();
        }else if(view == btn_register){
            String account = et_account.getText().toString();
            String password = et_password.getText().toString();

            //账号密码判断
            if(TextUtils.isEmpty(account) || TextUtils.isEmpty(password)){
                Toast.makeText(Register.this,"账号和密码不能为空！",Toast.LENGTH_LONG).show();
            }else{
                long rowId = userHelper.register(account,password);
                if(rowId < 0){
                    Toast.makeText(Register.this,"注册失败！",Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(Register.this,"注册成功！",Toast.LENGTH_LONG).show();
                    finish();
                }
            }
        }
    }
}